<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\service\analysis\data\pic;

use app\enum\CommonEnum;
use app\model\ClientAccountModel;
use app\model\mongo\MongoReportModel;
use app\service\analysis\report\ReportPicService;
use app\service\BaseService;
use app\service\client\AccountService;
use app\service\client\KuaiShouAccountService;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class PicReportFixService extends BaseService
{
    /**
     * @var int 媒体id
     */
    private int $mediaId;

    /**
     * @var null|ReportPicService 账户服务
     */
    private ?ReportPicService $service;

    /**
     * @var null|MongoReportModel 日报模型
     */
    private ?MongoReportModel $report;

    public function __construct(int $mediaId = 0)
    {
        $this->mediaId = $mediaId;
        $this->service = new ReportPicService($this->mediaId);
    }

    /**
     * 是否是日报服务
     */
    public function isService(): bool
    {
        return $this->service->isService();
    }

    /**
     * 获取待修复列表.
     *
     * @return array|Collection|MongoReportModel[]
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getList()
    {
        return $this->service->getFixDayList();
    }

    /**
     * 执行任务
     */
    public function runTask(MongoReportModel $model): bool
    {
        $this->report = $model;
        if (! $this->report->getKey()) {
            return self::setError('未知日报数据');
        }

        if (! $this->setFix()) {
            return $this->setReportDirty();
        }

        return $this->setReportSync();
    }

    /**
     * 数据修复.
     */
    private function setFix(): bool
    {
        if (empty($this->report->getAttr('stat_date'))) {
            return self::setError('未知汇总日期');
        }
        if (empty($this->report->getAttr('account_id'))) {
            return self::setError('未知账户id');
        }
        if (empty($this->report->getAttr('adv_id'))) {
            return self::setError('未知账户');
        }
        $account = AccountService::getAccountModel($this->report->getAttr('account_id'));
        if (! $account instanceof ClientAccountModel || ! $account->getKey()) {
            return self::setError('未知账户信息');
        }

        if (empty($this->report->getAttr('company_id'))) {
            $this->report->setAttr('company_id', $account->getAttr('company_id'));
        }
        if (empty($this->report->getAttr('client_id'))) {
            $this->report->setAttr('client_id', $account->getAttr('client_id'));
        }

        if ($this->mediaId == CommonEnum::KS_MEDIA && empty($this->report->getAttr('user_id'))) {
            $this->report->setAttr('user_id', intval(KuaiShouAccountService::getKsUserId($this->report->getAttr('adv_id'))));
        }

        if (empty($this->report->getAttr('signature'))) {
            $signature = $this->service->getSignature($this->report);
            if ($signature) {
                $this->report->setAttr('signature', $signature);
            } else {
                return self::setError('未知MD5信息');
            }
        }
        return true;
    }

    /**
     * 设置日报同步完成.
     */
    private function setReportSync(): bool
    {
        $this->report->setAttr('is_sync', MongoReportModel::SYNC_NO);
        $this->report->setAttr('is_archive', MongoReportModel::ARCHIVE_NO);
        return $this->report->save();
    }

    /**
     * 设置日报未脏数据.
     */
    private function setReportDirty(): bool
    {
        $this->report->setAttr('is_sync', MongoReportModel::SYNC_DIRTY);
        $this->report->save();
        return false;
    }
}
